package com.xiaoshuidi.cloud.module.bpm.service.sworecord;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.sworecord.vo.SwoTicketAssignRecordCreateReqVO;
import com.xiaoshuidi.cloud.module.bpm.convert.sworecord.SwoTicketAssignRecordConvert;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.sworecord.SwoTicketAssignRecordDO;
import com.xiaoshuidi.cloud.module.bpm.dal.mysql.sworecord.SwoTicketAssignRecordMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 服务工单记录 Service 实现类
 *
 * @author 合房租赁
 */
@Service
@Validated
public class SwoTicketAssignRecordServiceImpl implements SwoTicketAssignRecordService {

    @Resource
    private SwoTicketAssignRecordMapper swoTicketAssignRecordMapper;

    @Override
    public Long createSwoTicketAssignRecord(SwoTicketAssignRecordCreateReqVO createReqVO) {
        // 插入
        SwoTicketAssignRecordDO swoTicketAssignRecord = SwoTicketAssignRecordConvert.INSTANCE.convert(createReqVO);
        swoTicketAssignRecordMapper.insert(swoTicketAssignRecord);
        // 返回
        return swoTicketAssignRecord.getId();
    }

    @Override
    public List<SwoTicketAssignRecordDO> getByTicketId(Long ticketId) {
        LambdaQueryWrapper<SwoTicketAssignRecordDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SwoTicketAssignRecordDO::getSwoTicketId, ticketId);
        wrapper.orderByDesc(SwoTicketAssignRecordDO::getCreateTime);
        return swoTicketAssignRecordMapper.selectList(wrapper);
    }

    /**
     * 所有服务工单的第一条指派记录
     * @param swoTicketIds
     */
    @Override
    public List<SwoTicketAssignRecordDO> getAssignmentRecordByIds(List<Long> swoTicketIds) {
        return swoTicketAssignRecordMapper.getAssignmentRecordByIds(swoTicketIds);
    }


}
